//-rw-rw-r-- 2 zler zler 18 11月 20 09:19 hello.txt
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>

int main(int argc, char const *argv[])
{
        if(argc != 2){
                printf("./a.out file name \n");
                return -1;
        }
        //调用stat 得到文件属性信息
        struct stat sb;
        //stat(argv[1],  &sb); //穿透
        lstat(argv[1],  &sb); //不穿透
        //解析属性信息 ,st_mode, uid, gid, time
        //st_mode
        char stmode[11] = {0};
        memset(stmode, '-', sizeof(stmode)-1);

        if(S_ISREG(sb.st_mode)) stmode[0] = '-';
        if(S_ISDIR(sb.st_mode)) stmode[0] = 'd';
        if(S_ISCHR(sb.st_mode)) stmode[0] = 'c';
        if(S_ISBLK(sb.st_mode)) stmode[0] = 'b';
        if(S_ISFIFO(sb.st_mode)) stmode[0] = 'p';
        if(S_ISLNK(sb.st_mode)) stmode[0] = 'l';
        if(S_ISSOCK (sb.st_mode)) stmode[0] = 's';

        // 解析用户权限
        if(sb.st_mode & S_IRUSR ) stmode[1] = 'r';
        if(sb.st_mode & S_IWUSR ) stmode[2] = 'w';
        if(sb.st_mode & S_IXUSR ) stmode[3] = 'x';

        // 解析用户组权限
        if(sb.st_mode & S_IRGRP ) stmode[4] = 'r';
        if(sb.st_mode & S_IWGRP ) stmode[5] = 'w';
        if(sb.st_mode & S_IXGRP ) stmode[6] = 'x';

        // 解析其他组权限
        if(sb.st_mode & S_IROTH ) stmode[7] = 'r';
        if(sb.st_mode & S_IWOTH ) stmode[8] = 'w';
        if(sb.st_mode & S_IXOTH ) stmode[9] = 'x';

        //分析 用户名 组名可以通过函数获得 getpwuid,getgrgid
        //localtime 获取本地时间
        struct tm *filetm = localtime(&sb.st_atim.tv_sec);
        char timebuf[20] = {0};
        sprintf(timebuf, "%d月 %d %02d:%02d",
                 filetm->tm_mon+1, filetm->tm_mday, filetm->tm_hour, filetm->tm_min);

        printf("%s %ld %s %s %ld %s %s\n", stmode, sb.st_nlink, getpwuid(sb.st_uid)->pw_name,
                        getgrgid(sb.st_gid)->gr_name, sb.st_size, timebuf, argv[1]);
        return 0;
}
